package dasom.basic;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class JoinMemberV2
 */
@WebServlet(urlPatterns={"/member/new","/member/newOk","/member/readAll","/member/readOne","/member/modify","/member/modifyOk","/member/remove"})
public class JoinMemberV2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String DRV="oracle.jdbc.OracleDriver";
	private static final String URL="jdbc:oracle:thin:@//112.216.253.122/xe";
	private static final String USR="dds889";
	private static final String PWD="dds889";

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		 res.setContentType("text/html; charset=UTF-8");
		 String uri = req.getRequestURI();
	      System.out.println(req.getRequestURI());
	      
	      if (uri.endsWith("new")) {
		         JoinMemberV2Form(req,res);
		      } else if (uri.endsWith("newOK")) {
		         System.out.println("회원 추가완료");
		         //클라이언트의 요청이 post이므로 doPost에서 처리함
		      } else if (uri.endsWith("readAll")) {
		         System.out.println("회원 조회");
		      } else if (uri.endsWith("readOne")) {
		         System.out.println("회원 상세 조회");
		      } else if (uri.endsWith("modify")) {
		         System.out.println("회원 수정");
		      } else if (uri.endsWith("updateOK")) {
		         System.out.println("회원 수정완료");
		      } else if (uri.endsWith("delete")) {
		         System.out.println("회원 삭제");
		      }
	}

	private void JoinMemberV2Form(HttpServletRequest req,
			HttpServletResponse res) throws IOException {
		res.setContentType("text/html;charset=UTF-8");
		PrintWriter out=res.getWriter();
		
		
		out.print("<!DOTCYPE html>");
		out.print("<html>");
		out.print("<head><title>회원정보 입력하기");
		out.print("</title></head>");
		out.print("<body><h1>회원정보 입력하기<h1>");
		out.print("<form name='mb' method='post' action='JoinMemberNewOk'>");
		out.print("<div>아이디<input type='text' name='id'></div>");
		out.print("<div>비밀번호<input type='pwd' name='pwd'></div>");
		out.print("<div>비밀번호 확인<input type='repwd' name='repwd'></div>");
		out.print("<div>이름<input type='text' name='name'></div>");
		out.print("<div>전자우편<input type='text' name='zip'></div>");
		out.print("<div><button type='submit'>입력완료</button></div>");
		out.print("<div><button type='submit'>다시입력</button></div>");
		out.print("</form>");
		out.print("</body>");
		out.print("</html>");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		String uri = req.getRequestURI();
		System.out.println(uri);
		
		if(uri.endsWith("newOK")) {
			insertJoinMemberV2(req,res);
		}else if (uri.endsWith("readAll")) {
			//selectAllJoinMember(req,res);
		}else if (uri.endsWith("updateOk")) {
			updateOkJoinMember(req,res);
		}

}

	/*private void selectAllJoinMember(HttpServletRequest req,
			HttpServletResponse res) {
		String SQL="select mbno,id,pwd,name,zip from joinmember order by sjno desc";
		StringBuilder sb=null;
		Connection conn = null;
		PreparedStatement ps = null;
		//ResultSet rs= null;
		
		try{
			Class.forName(DRV);
			conn=DriverManager.getConnection(URL,USR,PWD);
			ps=conn.prepareStatement(SQL);
			rs=ps.executeQuery();
			
			sb=new StringBuilder();
			while (rs.next()) {
				sb.append("<div>").append(rs.getString(1)).append("");}}
						
			
		
		}	*/
	
	private void updateOkJoinMember(HttpServletRequest req,
			HttpServletResponse res) {
		// TODO Auto-generated method stub
		
	}


	private void insertJoinMemberV2(HttpServletRequest req,
			HttpServletResponse res) {
		// TODO Auto-generated method stub
		
	}
	
}